package com.example.lottery.common.utils;

import jakarta.mail.internet.MimeMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import java.util.Map;


@Slf4j
@Configuration
public class MailUtil {
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private MailProperties mailProperties;

    /**
     *
     * @param to 发给谁
     * @param content 正文
     */
    public void sendMail(String to, String content){
        try {
            // 创建一个邮件消息
            MimeMessage message = javaMailSender.createMimeMessage();
            // 创建 MimeMessageHelper
            MimeMessageHelper helper = new MimeMessageHelper(message, false);

            String from = mailProperties.getUsername();
            // 发件人邮箱和名称
            helper.setFrom(from, mailProperties.getProperties().getOrDefault("personal", from));
            // 收件人邮箱
            helper.setTo(to);
            // 邮件标题
            helper.setSubject(mailProperties.getProperties().getOrDefault("subject", "验证码"));
            // 邮件正文，第二个参数表示是否是HTML正文
            helper.setText(content, true);
            // 发送
            javaMailSender.send(message);
        }catch (Exception e){
            log.error("邮件发送失败, e:", e);
        }

    }
//    public void sendMailToPrize(String to, String content){
//        try {
//            // 创建一个邮件消息
//            MimeMessage message = javaMailSender.createMimeMessage();
//            // 创建 MimeMessageHelper
//            MimeMessageHelper helper = new MimeMessageHelper(message, false);
//
//            String from = mailProperties.getUsername();
//            // 发件人邮箱和名称
//            helper.setFrom(from, mailProperties.getProperties().getOrDefault("personal", from));
//            // 收件人邮箱
//            helper.setTo(to);
//            // 邮件标题
//            helper.setSubject(mailProperties.getProperties().getOrDefault("subject", "中奖通知"));
//            // 邮件正文，第二个参数表示是否是HTML正文
//            helper.setText(content, true);
//            // 发送
//            javaMailSender.send(message);
//        }catch (Exception e){
//            log.error("邮件发送失败, e:", e);
//        }
//
//    }
    public void sendMailToPrize(String to, String content) {
        try {
            // 创建一个邮件消息
            MimeMessage message = javaMailSender.createMimeMessage();
            // 创建 MimeMessageHelper
            MimeMessageHelper helper = new MimeMessageHelper(message, false);

            String from = mailProperties.getUsername();
            // 发件人邮箱和名称
            helper.setFrom(from, mailProperties.getProperties().getOrDefault("personal", from));
            // 收件人邮箱
            helper.setTo(to);
            // 强制设置 "中奖通知" 作为主题，而不是依赖全局配置
            helper.setSubject("中奖通知");
            // 邮件正文，第二个参数表示是否是HTML正文
            helper.setText(content, true);
            // 发送
            javaMailSender.send(message);
        } catch (Exception e) {
            log.error("邮件发送失败, e:", e);
        }
    }
}
